// pages/binding/index.js
const app = getApp()
import {
  Index
} from 'index-model.js'
var ajax = new Index(); // 实例化

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',//手机号
    code: '',//验证码
    codename: '获取短信验证码',
    disabled:false // 是否禁用登录按钮
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //获取input 输入框的值
  getPhoneValue(e){
    this.setData({
      phone:e.detail.value
    })
  },

  // 获取验证码
  getCodeValue(e){
    let that = this;
    this.setData({
      code:e.detail.value
    })
  },

  // 获取验证码
  getVerificationCode(){
    let that = this;
    let phoneStatus = that.getCode();
    let phone = that.data.phone;
    if (phoneStatus){
      ajax.getVerificationCodeData(phone,(res) => {
        console.log(res)
        if (res.data.Code == "OK") {
          wx.showToast({
            title: '验证码已发送'
          })
          that.setTimeOutCode();
        }
      })
    }
  },
  
  // 验证码倒计时
  setTimeOutCode(){
    let that = this;
    let num = 61;
    let timer = setInterval(function () {
      num--;
      if (num <= 0) {
        clearInterval(timer);
        that.setData({
          codename: '重新发送',
          disabled: false
        })
      } else {
        that.setData({
          codename: num + 's'
        })
      }
    }, 1000)
  },

  // 判断手机号
  getCode(){
    let that = this;
    let code = true;
    let phone = this.data.phone;
    if (phone == "" || phone == null){
      wx.showToast({
        title: '手机号码不能为空',
        icon:'none'
      })
      code = false;
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(phone))){
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
      code = false;
      return false;
    }
    return code;
  },

  // 登录方法
  submitLogin(e){
    let that = this;
    let code = that.getCode();
    let merter = {
      phone: that.data.phone,
      code: that.data.code
    }
    if (code){
      ajax.getValidateVerificationCodeData(merter,(res) => {
        console.log(res)
        if (res.status == 1) {
          wx.setStorageSync('token', res.data.accessToken);
          wx.setStorageSync('refereeData', res.data);
          wx.reLaunch({
            url: '../index/index',
          })
        }
      })
    }
    
    

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})